package org.bulldog.playlist;

import org.bulldog.playlist.message.MessagePlayList;
import org.bulldog.playlist.notice.Notice;
import org.bulldog.playlist.notice.NoticePlayList;
import org.bulldog.playlist.message.Message;
import org.bulldog.playlist.program.Program;
import org.bulldog.playlist.program.ProgramPlayList;
import org.bulldog.playlist.util.ThreadUtil;

import java.util.*;


/**
 * 播表播放器(线程安全) 非堵塞
 *
 * @author zhanghongbin
 * @version 1.0
 */
public final class PlayListPlayer {

    public static final PlayListPlayer INSTANCE = new PlayListPlayer();

    private PlayListener playListener = null;

    private PlayListPlayer() {
        this.playListener = new LoggingPlayListener();
        this.initProgram();
        this.initNotice();
    }

    private void initProgram() {
        //非堵塞
        ThreadUtil.execute(new Runnable() {
            @Override
            public void run() {
                /**
                 * 初始化的时候不需要执行播表，当有存在播表数据的时候才开始执行
                 */
                synchronized (ProgramPlayList.class) {
                    try {
                        //一定要使用while 不要使用if 因为ProgramPlayList.class.notifyAll 有可能被调用最好
                        //在这里被阻塞
                        while (ProgramPlayList.isEmpty()) { //如果节目播表没有数据则等待，防止循环空转
                            ProgramPlayList.class.wait();
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                ProgramPlayList.run(playListener);
            }
        });
    }

    private void initNotice() {
        ThreadUtil.execute(new Runnable() {
            @Override
            public void run() {
                synchronized (NoticePlayList.class) {
                    try {
                        while (NoticePlayList.isEmpty()) { //如果通知播表没有数据则等待，防止循环空转
                            NoticePlayList.class.wait();
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                NoticePlayList.run(playListener);
            }
        });
    }

    /**
     * 设置播放监听器
     *
     * @param playListener
     */
    public void setPlayListener(PlayListener playListener) {
        this.playListener = playListener;
    }

    /**
     * 轮播节目
     *
     * @param dailyProgramList  日常节目
     * @param sportsProgramList 插播节目
     */
    public void play(List<Program> dailyProgramList, List<Program> sportsProgramList) {
        this.play(dailyProgramList, sportsProgramList, new List[]{});
    }

    /**
     * 轮播节目
     *
     * @param dailyProgramList        日常节目
     * @param sportsProgramList       插播节目
     * @param sportsDailyProgramArray 日常插播节目
     */
    public void play(List<Program> dailyProgramList, List<Program> sportsProgramList, List<Program>[] sportsDailyProgramArray) {
        ProgramPlayList.build(dailyProgramList, sportsProgramList, sportsDailyProgramArray, playListener);
    }

    /**
     * 轮播节目
     *
     * @param sportsProgramList       插播节目
     * @param sportsDailyProgramArray 日常插播节目
     */
    public void play(List<Program> sportsProgramList, List<Program>[] sportsDailyProgramArray) {
        this.play(new ArrayList(), sportsProgramList, sportsDailyProgramArray);
    }

    /**
     * 轮播通知
     *
     * @param noticeList
     */
    public void play(List<Notice> noticeList) {
        NoticePlayList.build(noticeList, playListener);
    }

    /**
     * 点播
     *
     * @param message
     */
    public void play(final Message message) {
        ThreadUtil.execute(new Runnable() {
            @Override
            public void run() {
                MessagePlayList.run(message, playListener);
            }
        });
    }

    /**
     * 每天凌晨执行定时任务
     *
     * @param timerTask
     */

    public void play(TimerTask timerTask) {
        PlayListTimerTask.execute(timerTask);
    }

}
